package com.xfly.app.ui;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.TimePicker;

import com.ptmall.app.R;
import com.ptmall.app.base.BaseMvpActivity;
import com.ptmall.app.net.api.ApiDataRepository;
import com.ptmall.app.net.base.ApiNetResponse;
import com.ptmall.app.utils.AllUtils;
import com.ptmall.app.utils.TimeUtil;
import com.ptmall.app.view.TitleBarView;
import com.xfly.app.bean.MsBean;
import com.xfly.app.bean.YhqBean;

import java.io.Serializable;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

public class MsActivity extends BaseMvpActivity implements View.OnClickListener {

    TitleBarView title_bar;
    TextView time1,time2,msnum;
    Calendar calendar= Calendar.getInstance(Locale.CHINA);
    MsBean mMsBean;
    ApiDataRepository apiDataRepository = ApiDataRepository.getInstance();
    Button lg_btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setView(R.layout.activity_ms);
    }

    @Override
    public void initViews() {
        title_bar = findViewById(R.id.title_bar);
        time1 = findViewById(R.id.time1);
        time2 = findViewById(R.id.time2);
        msnum = findViewById(R.id.msnum);
        lg_btn = findViewById(R.id.lg_btn);

        title_bar.setText("限时秒杀");
        title_bar.setRightText("修改");
    }

    @Override
    public void initEvents() {
        title_bar.setOnTitleBarClickListener(new TitleBarView.TitleBarClickListener() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {
                startActivity(new Intent(getContext(),MsSetActivity.class));
            }
        });
        time1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
                    // 绑定监听器(How the parent is notified that the date is set.)
                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        // 此处得到选择的时间，可以进行你想要的操作
//                        tv.setText("您选择了：" + year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");


                        final Calendar calendar = Calendar.getInstance();
                        int hour = calendar.get(Calendar.HOUR_OF_DAY);
                        int minute = calendar.get(Calendar.MINUTE);
                        //实例化TimePickerDialog对象
                        final TimePickerDialog timePickerDialog = new TimePickerDialog(getContext(), new TimePickerDialog.OnTimeSetListener() {
                            //选择完时间后会调用该回调函数
                            @Override
                            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                                time1.setText(year+"-"+(monthOfYear+1)+"-"+dayOfMonth+" "+hourOfDay+":"+minute);
                                //设置TextView显示最终选择的时间
//                                select_time.setText(hourOfDay + ":" + minute);
                            }
                        }, hour, minute, true);
                        timePickerDialog.show();

                    }
                }
                        // 设置初始日期
                        , calendar.get(Calendar.YEAR)
                        , calendar.get(Calendar.MONTH)
                        , calendar.get(Calendar.DAY_OF_MONTH));
            }
        });
        time2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
                    // 绑定监听器(How the parent is notified that the date is set.)
                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        // 此处得到选择的时间，可以进行你想要的操作
//                        tv.setText("您选择了：" + year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");


                        final Calendar calendar = Calendar.getInstance();
                        int hour = calendar.get(Calendar.HOUR_OF_DAY);
                        int minute = calendar.get(Calendar.MINUTE);
                        //实例化TimePickerDialog对象
                        final TimePickerDialog timePickerDialog = new TimePickerDialog(getContext(), new TimePickerDialog.OnTimeSetListener() {
                            //选择完时间后会调用该回调函数
                            @Override
                            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                                time2.setText(year+"-"+(monthOfYear+1)+"-"+dayOfMonth+" "+hourOfDay+":"+minute);
                                //设置TextView显示最终选择的时间
//                                select_time.setText(hourOfDay + ":" + minute);
                            }
                        }, hour, minute, true);
                        timePickerDialog.show();

                    }
                }
                        // 设置初始日期
                        , calendar.get(Calendar.YEAR)
                        , calendar.get(Calendar.MONTH)
                        , calendar.get(Calendar.DAY_OF_MONTH));
            }
        });
        msnum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(),MzGoodListActivity.class);
                intent.putExtra("goodlist", (Serializable) mMsBean.getGoods());
                startActivity(intent);

            }
        });
        lg_btn.setVisibility(View.GONE);
        getData();
    }


    private void getData(){
        apiDataRepository.getmslist(null, new ApiNetResponse<MsBean>() {
            @Override
            public void onSuccess(MsBean data) {
                mMsBean = data;
                time1.setText(TimeUtil.getStrTime(data.getStime()+""));
                time2.setText(TimeUtil.getStrTime(data.getEtime()+""));
                msnum.setText(data.getGoods().size()+"件商品");
            }
        });
    }

    @Override
    public void onClick(View view) {

    }
}
